package com.kld.test;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;


import java.io.File;
/**
 * @author ocean
 * @date 2024/05/06
 */
public class SysUtils
{


    public static void silenceInstall(Context context, String appFilePath) {
        File apkFile = new File(appFilePath);
        if (!apkFile.exists()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent installApkIntent = new Intent();
            installApkIntent.setAction(Intent.ACTION_VIEW);
            installApkIntent.addCategory(Intent.CATEGORY_DEFAULT);
            installApkIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            FileProvider7.setIntentDataAndType(context, installApkIntent, "application/vnd.android.package-archive", apkFile, true);
            //设置静默安装标识
            installApkIntent.putExtra("silence_install", true);
            //设置安装完成是否启动标识
            installApkIntent.putExtra("is_launch", true);
            //设置后台中启动activity标识
            installApkIntent.putExtra("allowed_Background", true);
            if (context.getPackageManager().queryIntentActivities(installApkIntent, 0).size() > 0) {
                context.startActivity(installApkIntent);
            }
        }
    }

    public static boolean silenceUninstall(Context context, String packageName) {
        if (null == packageName || "".equals(packageName)) {
            return false;
        }

        try {
            PackageInfo packinfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
            String[] list = packinfo.requestedPermissions;
            boolean hasPermission = false;
            if (null != list) {
                for (int i = 0; i < list.length; i++) {
                    if (Manifest.permission.QUERY_ALL_PACKAGES.equals(list[i])) {
                        hasPermission = true;
                        break;
                    }
                }
            }
            if (!hasPermission) {
                throw new RuntimeException("need permission " + Manifest.permission.QUERY_ALL_PACKAGES);
            }
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }

        if (!checkExistForApp(context, packageName)) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_DELETE);
        intent.setData(Uri.parse("package:" + packageName));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("allowed_Background", true);
        intent.putExtra("silence_install", true);
        context.startActivity(intent);

        return true;
    }

    private static boolean checkExistForApp(Context context, String packageName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        return packageInfo != null;
    }
}
